该功能特性从 v2.4 版本开始提供。
基本介绍
该命令用于读取配置的数据库,根据数据表生成对应的 proto 数据结构文件。
命令使用
$ gf gen pbentity -h
USAGE
gf gen pbentity [OPTION]
OPTION
-p, --path directory path for generated files storing
-k, --package package path for all entity proto files
-l, --link database configuration, the same as the ORM configuration of GoFrame
-t, --tables generate models only for given tables, multiple table names separated with ','
-f, --prefix add specified prefix for all entity names and entity proto files
-r, --removePrefix remove specified prefix of the table, multiple prefix separated with ','
-n, --nameCase case for message attribute names, default is "Camel":
| Case | Example |
|---------------- |--------------------|
| Camel | AnyKindOfString |
| CamelLower | anyKindOfString | default
| Snake | any_kind_of_string |
| SnakeScreaming | ANY_KIND_OF_STRING |
| SnakeFirstUpper | rgb_code_md5 |
| Kebab | any-kind-of-string |
| KebabScreaming | ANY-KIND-OF-STRING |
-j, --jsonCase case for message json tag, cases are the same as "nameCase", default "CamelLower".
set it to "none" to ignore json tag generating.
-o, --option extra protobuf options
-h, --help more information about this command
EXAMPLE
gf gen pbentity
gf gen pbentity -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
gf gen pbentity -p ./protocol/demos/entity -t user,user_detail,user_login
gf gen pbentity -r user_ -k github.com/gogf/gf/example/protobuf
gf gen pbentity -r user_
CONFIGURATION SUPPORT
Options are also supported by configuration file.
It's suggested using configuration file instead of command line arguments making producing.
The configuration node name is "gf.gen.pbentity", which also supports multiple databases, for example(config.yaml):
gfcli:
gen:
- pbentity:
link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
path: "protocol/demos/entity"
tables: "order,products"
package: "demos"
- pbentity:
link: "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
path: "protocol/demos/entity"
prefix: "primary_"
tables: "user, userDetail"
package: "demos"
option: |
option go_package = "protobuf/demos";
option java_package = "protobuf/demos";
option php_namespace = "protobuf/demos";
如果使用框架推荐的项目工程脚手架,并且系统安装了 make 工具,也可以使用 make pbentity 快捷指令。
参数说明:
| 名称 | 默认值 | 含义 | 示例 |
|---|---|---|---|
gfcli.gen.pbentity | 代码生成配置项,可以有多个配置项构成数组,支持多个数据库生成。不同的数据库可以设置不同的生成规则,例如可以生成到不同的位置或者文件。 | - | |
path | manifest/protobuf/pbentity | 生成 proto 文件的存储 目录 地址。 | protobuf/pbentity |
package | 自动识别 go.mod | 生成的 proto 文件中的 go_package 路径,并自动识别 package 名称 | - |
link | 分为两部分,第一部分表示你连接的数据库类型 mysql, postgresql 等, 第二部分就是连接数据库的 dsn 信息。具体请参考 ORM使用配置 章节。 | - | |
prefix | 生成数据库对象及文件的前缀,以便区分不同数据库或者不同数据库中的相同表名,防止数据表同名覆盖。 | order_user_ | |
removePrefix | 删除数据表的指定前缀名称。多个前缀以 , 号分隔。 | gf_ | |
tables | 指定当前数据库中需要执行代码生成的数据表。如果为空,表示数据库的所有表都会生成。 | user, user_detail | |
nameCase | CamelLower | 生成的 message 属性字段名称格式。参数可选为: Camel、 CamelLower、 Snake、 SnakeScreaming、 SnakeFirstUpper、 Kebab、 KebabScreaming。具体介绍请参考命名行帮助示例。 | Snake |
option | 额外的 proto option 配置列表 |
与 gen dao 中的 entity 差别
相同之处
- 两者生成的内容都是
entity内容,即从数据集合(数据库表)中生成对应的Golang实体对象供程序方便使用。并且都是单向生成,即只能从数据集合生成实体对象代码,以保证实体对象数据结构的同步。 gen dao生成的entity数据实体对象是对于Golang语言来说是通用的,但目前主要为HTTP协议服务。在HTTP服务中,gen dao中生成的entity虽然是在internal目录下,但最终也会作为HTTP API返回的一部分服务客户端。
不同之处
- 在
GRPC服务中,gen dao生成的entity数据结构无法提供给GRPC接口使用,因为GRPC的数据结构需要使用proto文件来定义。因此,在GRPC服务中就需要使用到gen pbentity中生成的pbentity proto文件。同时,在GRPC微服务开发中,gen dao生成的entity已经没有具体作用。 - 取名
pbentity而不是entity的名称,是为了防止和gen dao中的entity含义冲突。